package com.book.app.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.app.aop.SystemControllerLog;
import com.book.app.entity.Category;
import com.book.app.service.CategoryService;
import com.book.app.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "图书类别模块")
@RestController
@RequestMapping("category")
@CrossOrigin //前后端分离配置跨域访问
public class CategoryController  {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("初始化馆藏图书添加时下拉列表表单的")
    @SystemControllerLog(description ="初始化馆藏图书添加时下拉列表表单的")
    @GetMapping("/findAll")
    public CommonResult findAll(){
     return CommonResult.success(categoryService.list());
    }

    @ApiOperation("通过类别名称查询类别ID")
    @SystemControllerLog(description ="通过类别名称查询类别ID")
    @GetMapping("/findIdByName")
    public CommonResult findIdByName(String catename){
        return CommonResult.success(categoryService.findIdByName(catename));
    }


    @ApiOperation("添加图书类别信息")
    @SystemControllerLog(description ="添加图书类别信息")
    @PostMapping("/save")
    public CommonResult save(@RequestBody Category category){
        category.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        boolean flag= categoryService.save(category);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("分页查询图书类别信息")
    @SystemControllerLog(description ="分页查询图书类别信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始数据",required = true,example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页显示分页最大数据",required = true,example = "10"),
            @ApiImplicitParam(name = "cname",value = "根据类别名称条件查询",example = "科幻小说类"),
    })
    @GetMapping("/findPager")
    public CommonResult findPager(Integer page, Integer limit,String cname){
        //条件查询
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        if(!"".equals(cname)&&cname!=null){
            queryWrapper.like(true,"CATENAME",cname);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("count",categoryService.count(queryWrapper));
        map.put("data",categoryService.page(new Page<Category>(page,limit),queryWrapper).getRecords());
        return CommonResult.success(map);
    }

    @ApiOperation("批量删除图书类别信息")
    @SystemControllerLog(description ="批量删除图书类别信息")
    @PostMapping("/removeBatch")
    public CommonResult removeBatch(@RequestParam("cateid[]") List<Integer> cateid){
        boolean flag=  categoryService.removeByIds(cateid);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("根据ID删除图书类别信息")
    @SystemControllerLog(description ="根据ID删除图书类别信息")
    @PostMapping("/remove")
    public CommonResult remove(@RequestParam("cateid") Integer cateid){
        boolean flag = categoryService.removeById(cateid);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("更新图书类别信息")
    @SystemControllerLog(description ="更新图书类别信息")
    @PostMapping("/update")
    public  CommonResult update(@RequestBody Category category){
        category.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date()));
        boolean flag= categoryService.updateById(category);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("查询各类别的图书个数")
    @SystemControllerLog(description ="查询各类别的图书个数")
    @GetMapping("/getCateBook")
    public  CommonResult getCateBook(){
        return CommonResult.success(categoryService.getCateBook());
    }

}

